package com.zxk.api.service.impl;

import com.zxk.api.dao.FundAnalysiseDao;
import com.zxk.api.dto.FundAnalysiseDto;
import com.zxk.api.service.FundAnalysiseService;
import com.zxk.core.common.model.FundAnalysise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zxk
 * @description 新闻业务层
 * @date 2020/8/28
 */
@Service
public class FundAnalysiseServiceImpl implements FundAnalysiseService {
    @Autowired
    private FundAnalysiseDao fundAnalysiseDao;


    @Override
    @Transactional
    public void save(FundAnalysise fundAnalysise) {
        //保存之前查看是否有重复数据

            fundAnalysiseDao.save(fundAnalysise);

    }

    @Override
    public List<FundAnalysise> find(FundAnalysise fundAnalysise) {
        Example<FundAnalysise> example = Example.of(fundAnalysise);
        List<FundAnalysise> fundAnalysises = fundAnalysiseDao.findAll(example);
        return fundAnalysises;
    }

    @Override
    public List<FundAnalysise> findNewsByTime(Date date) {
//        https://blog.csdn.net/long476964/article/details/79677526
//        News news = new News();
//        news.setCreateTime(date);
//        List<News> list = this.fundAnalysiseDao.selectNewsByTime(date);
        return null;
    }

    @Override
    public List<FundAnalysise> findAll() {
        List<FundAnalysise> list = this.fundAnalysiseDao.findAll();
        return list;
    }

    @Override
    public List<FundAnalysiseDto> findByTimeBlur(FundAnalysise fundAnalysise) {
        String fundCode = fundAnalysise.getFundCode();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if(null != fundAnalysise.getCreateTime()) {
            String date = sdf.format(fundAnalysise.getCreateTime());
            List<FundAnalysise> fundAnalysiseList = this.fundAnalysiseDao.findByTimeBlur(fundCode,date);
            List<FundAnalysiseDto> fundAnalysiseDtos = new ArrayList<>();
            if(null != fundAnalysiseList && fundAnalysiseList.size() > 0) {
                FundAnalysiseDto fundAnalysiseDto = null;
                for (FundAnalysise analysise : fundAnalysiseList) {
                    fundAnalysiseDto = new FundAnalysiseDto(analysise);
                    fundAnalysiseDtos.add(fundAnalysiseDto);
                }
            }
            return fundAnalysiseDtos;
        }
        return null;
    }
}
